import 'package:flutter_admin/models/odoo/mps_transit.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/utils.dart';
import 'package:intl/intl.dart';
import 'package:quiver/strings.dart';
import '../../../generated/l10n.dart';

class MpsTransitDialog extends StatefulWidget {
  MpsTransitDialog();

  @override
  _MpsTransitDialogState createState() => _MpsTransitDialogState();
}

class _MpsTransitDialogState extends State<MpsTransitDialog> {
  // late List<Widget> children;
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  DateFormat format = DateFormat('yyyy-MM-dd');

  MpsTransit mpsTransit = MpsTransit();
  List<SelectOptionVO> alreadyDoneList = [];

  @override
  void initState() {
    super.initState();
    mpsTransit.createDateStart = Utils.getDate();
    mpsTransit.createDateEnd = Utils.getDate();
    _loadData();
  }

  _loadData() async {
    alreadyDoneList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: 'true', label: "是"),
      SelectOptionVO(value: 'false', label: "否"),
    ];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelectDate(
            context,
            width: 280,
            label: '开始时间',
            value: mpsTransit.createDateStart,
            lastDate: isNotBlank(mpsTransit.createDateEnd)
                ? format.parse(mpsTransit.createDateEnd!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsTransit.createDateStart = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsTransit.createDateStart = v;
            },
          ),
          CrySelectDate(
            context,
            width: 280,
            label: '结束时间',
            value: mpsTransit.createDateEnd,
            firstDate: isNotBlank(mpsTransit.createDateStart)
                ? format.parse(mpsTransit.createDateStart!)
                : null,
            onDateChange: (v) {
              formKey.currentState?.save();
              mpsTransit.createDateEnd = v;
              setState(() {});
            },
            onSaved: (v) {
              mpsTransit.createDateEnd = v;
            },
          ),
          CryInput(
            width: 280,
            label: '产品名称',
            value: mpsTransit.productName,
            onSaved: (v) {
              mpsTransit.productName = v;
            },
          ),
          CryInput(
            width: 280,
            label: '规格型号',
            value: mpsTransit.description,
            onSaved: (v) {
              mpsTransit.description = v;
            },
          ),
          CrySelect(
            width: 280,
            label: '是否已完成',
            dataList: alreadyDoneList,
            value: mpsTransit.alreadyDone,
            onSaved: (v) {
              mpsTransit.alreadyDone = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            form.save();
            Navigator.pop(context, mpsTransit);
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text("查询"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
